#include <stdio.h>
#include "alengine.h"
#include <unistd.h>
#include <cstdlib>
#include <time.h>
using namespace std;
#define MAXN 2
ptr_t channels[MAXN];

void volume_test(oal_engine *t)
{
    fprintf(stderr, "Volume test\n");
    sleep(2);
    for (int i = 0; i < MAXN; i++)
        t->channel_set_volume(channels[i], 0); // muting
    sleep(2);

    for (int j = 0; j < 2; j++)
    {
        for (int i = 0; i < MAXN; i++)
        {
            int volume = rand() % 101;
            fprintf(stderr, "Channel #%d volume now == %d\n", i, volume);
            t->channel_set_volume(channels[i], volume);
        }
        sleep(3);
    }
}

void multichannels_test(oal_engine *t)//casting multichannels
{
    fprintf(stderr, "Casting channels\n");
    channels[0] = t->create_channel("/windows/D/Music/a.ogg", 0, 1);
    if (MAXN > 1)
        channels[1] = t->create_channel("/windows/D/Music/original.mp3", 0, 1);
    if (MAXN > 2)
    channels[2] = t->create_channel("/home/lberserq/audio/Shimokawakiminoyume.mp3", 0, 1);
    if (MAXN > 3)
        channels[3] = t->create_channel("/windows/D/Music/Unforgiven.mp3", 0, 1);
    for (int i = 0; i < MAXN; i++)
    {
        fprintf(stderr, "Channel #%d  Now have handle %lu\n", i, channels[i]);
    }
}

void length_test(oal_engine *t)
{
    fprintf(stderr, "Length test\n");
    for (int i = 0; i < MAXN; i++)
        fprintf(stderr, "Channel #%d  Handle %llx Length == %02d:%02d\n", i, channels[i], t->channel_get_length(channels[i]) / 60, t->channel_get_length(channels[i]) % 60);
}

void current_pos_test(oal_engine *t)
{
    fprintf(stderr, "Current position test\n");
    for (int k = 0; k < 1000; k++)
    {
        for (int i = 0; i < MAXN; i++)
        {
            QWORD d = t->channel_get_pos(channels[i]);
            fprintf(stderr, "Channel #%d Handle %llx position ==  %02lu:%02lu\n", i, channels[i], d / 60, d % 60);
        }
        sleep(1);
        fprintf(stderr, "\n\n\n\n\n\n\n\n\n");
        //ffprintf(stderr, ("RealSec =  %d\n", k * 2);
    }
}

void terminate_test(oal_engine *t)//casting multichannels
{
    fprintf(stderr, "Terminating channels\n");
    for (int i = 0; i < MAXN; i++)
    {
        t->terminate_channel(channels[i]);
        channels[i] = 0;
    }
}

int main()
{
    srand(time(0));
    oal_engine *t = new oal_engine;
    //multichannels_test(t);
    //length_test(t);
    //sleep(5);
    //t.channel_set_pos(channels[0], OAL_SECONDS2BYTES(30.0));
    //volume_test(t);
    for (int i = 1; i < 3; i++)
    {
        multichannels_test(t);
        sleep(10);
        terminate_test(t);
        sleep(3);
    }

    multichannels_test(t);
    volume_test(t);
    //current_pos_test(t);
    for (int i = 0; i < MAXN; i++)
        t->terminate_channel(channels[i]);
    delete t;
    return 0;
}

